﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Text.RegularExpressions;

namespace doondo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string ConnectString() {
            string strConn;
            strConn = "Data Source=WALLACE2-PC\\SQLEXPRESS;Initial Catalog=searchDB;Integrated Security=True;";
            return strConn;
        }

        protected void btnDoond_Click(object sender, EventArgs e)
        {

            //To search for the searchtext if contains OR operator
            string searchORText = tbxDoond.Text;
            searchORText = searchORText.ToLower();
            string advanceORText;
            int intSearchOr;
            intSearchOr = searchORText.IndexOf(" or ");
            advanceORText = searchORText.Replace(" or ", "%' or fileContent like '%");

            //To search for the searchtext if contains AND operator
            string searchANDText = tbxDoond.Text;
            searchANDText = searchORText.ToLower();
            string advanceANDText;
            int intSearchAnd;
            intSearchAnd = searchANDText.IndexOf(" and ");
            advanceANDText = searchANDText.Replace(" and ", "%' and fileContent like '%");

            //To search for the searchtext if contains - operator
            string searchMINUSText = tbxDoond.Text;
            searchMINUSText = searchMINUSText.ToLower();
            string advanceMINUSText;
            int intSearchMinus;
            intSearchMinus = searchMINUSText.IndexOf(" - ");
            advanceMINUSText = searchMINUSText.Replace(" - ", "%' and fileContent not like '%");

            //To search for the searchtext if contains + operator
            string searchPLUSText = tbxDoond.Text;
            searchPLUSText = searchPLUSText.ToLower();
            string advancePLUSText;
            int intSearchPlus;
            intSearchPlus = searchPLUSText.IndexOf(" + ");
            advancePLUSText = searchPLUSText.Replace(" + ", "%' and fileContent like '%");

            //To search for the searchtext if contains "" operators
            string openCommas, advanceOCommas;
            int intSearchOCommas;
            openCommas = tbxDoond.Text.ToLower();
            intSearchOCommas = openCommas.IndexOf("“");
            advanceOCommas = openCommas.Replace("“", "'%");
            

            if (tbxDoond.Text != "")
            {

                SqlConnection conn = new SqlConnection();
                SqlCommand comm = new SqlCommand();
                try
                {
                    if (intSearchOr != -1)
                    {//if the search field contains OR

                        conn.ConnectionString = ConnectString();
                        conn.Open();
                        comm.Connection = conn;
                        string Pname = tbxDoond.Text;
                        string Sname = comm.CommandText = "Select * from fileTable where fileContent like '%" + advanceORText + "%'";
                        SqlDataReader dr = comm.ExecuteReader();
                        DoondList.DataSource = dr;


                        DoondList.DataBind();
                        dr.Close();
                        comm.CommandText = "Select * from fileTable where fileContent like '%" + advanceORText + "%'";
                        SqlDataReader drAdv1 = comm.ExecuteReader();
                        bool found = false;
                        int count = 0;
                        while (drAdv1.Read())
                        {
                            found = true;
                            count++;
                        }
                        if (!found)
                        {
                            lblError.Text = count + " Record(s) Found";
                        }
                        else
                        {
                            lblError.Text = count + " Record(s) Found For " + searchORText;

                        }

                    }
                    else if (intSearchAnd != -1)
                    {
                        conn.ConnectionString = ConnectString();
                        conn.Open();
                        comm.Connection = conn;
                        string Pname = tbxDoond.Text;
                        string Sname = comm.CommandText = "Select * from fileTable where fileContent like '%" + advanceANDText + "%'";
                        SqlDataReader dr = comm.ExecuteReader();
                        DoondList.DataSource = dr;
                        DoondList.DataBind();
                        dr.Close();
                        comm.CommandText = "Select * from fileTable where fileContent like '%" + advanceANDText + "%'";
                        SqlDataReader drAdv1 = comm.ExecuteReader();
                        bool found = false;
                        int count = 0;
                        while (drAdv1.Read())
                        {
                            found = true;
                            count++;
                        }
                        if (!found)
                        {
                            lblError.Text = count + " Record(s) Found";
                        }
                        else
                        {
                            lblError.Text = count + " Record(s) Found For " + searchANDText;

                        }

                    }else if(intSearchPlus != -1)
                    {
                        conn.ConnectionString = ConnectString();
                        conn.Open();
                        comm.Connection = conn;
                        string Pname = tbxDoond.Text;
                        string Sname = comm.CommandText = "Select * from fileTable where fileContent like '%" + advancePLUSText + "%'";
                        SqlDataReader dr = comm.ExecuteReader();
                        DoondList.DataSource = dr;
                        DoondList.DataBind();
                        dr.Close();
                        comm.CommandText = "Select * from fileTable where fileContent like '%" + advancePLUSText + "%'";
                        SqlDataReader drAdv1 = comm.ExecuteReader();
                        bool found = false;
                        int count = 0;
                        while (drAdv1.Read())
                        {
                            found = true;
                            count++;
                        }
                        if (!found)
                        {
                            lblError.Text = count + " Record(s) Found";
                        }
                        else
                        {
                            lblError.Text = count + " Record(s) Found For " + searchPLUSText;

                        }

                    }
                    else if(intSearchMinus != -1)
                    {

                        conn.ConnectionString = ConnectString();
                        conn.Open();
                        comm.Connection = conn;
                        string Pname = tbxDoond.Text;
                        string Sname = comm.CommandText = "Select * from fileTable where fileContent like '%" + advanceMINUSText + "%'";
                        SqlDataReader dr = comm.ExecuteReader();
                        DoondList.DataSource = dr;
                        DoondList.DataBind();
                        dr.Close();
                        comm.CommandText = "Select * from fileTable where fileContent like '%" + advanceMINUSText + "%'";
                        SqlDataReader drAdv1 = comm.ExecuteReader();
                        bool found = false;
                        int count = 0;
                        while (drAdv1.Read())
                        {
                            found = true;
                            count++;
                        }
                        if (!found)
                        {
                            lblError.Text = count + " Record(s) Found";
                        }
                        else
                        {
                            lblError.Text = count + " Record(s) Found For " + searchMINUSText;

                        }

                    }else if(intSearchOCommas != -1)
                    {
                        string closeCommas, advanceCCommas;
                        int intSearchCCommas;
                        closeCommas = advanceOCommas;
                        intSearchCCommas = closeCommas.IndexOf("”");
                        advanceCCommas = closeCommas.Replace("”", "%'");

                        conn.ConnectionString = ConnectString();
                        conn.Open();
                        comm.Connection = conn;
                        string Pname = tbxDoond.Text;
                        string Sname = comm.CommandText = "select * from fileTable where fileContent like " + advanceCCommas + " ";
                        SqlDataReader dr = comm.ExecuteReader();
                        DoondList.DataSource = dr;


                        DoondList.DataBind();
                        dr.Close();
                        comm.CommandText = "Select * from fileTable where fileContent like " + advanceCCommas + " ";
                        SqlDataReader dr1 = comm.ExecuteReader();
                        bool found = false;
                        int count = 0;
                        while (dr1.Read())
                        {
                            found = true;
                            count++;
                        }
                        if (!found)
                        {
                            lblError.Text = count + " Record(s) Found";
                        }
                        else
                        {

                            lblError.Text = count + " Record(s) Found For " + advanceCCommas;
                        }
                
                    }
                    else 
                    {
                        conn.ConnectionString = ConnectString();
                        conn.Open();
                        comm.Connection = conn;
                        string Pname = tbxDoond.Text;
                        string Sname = comm.CommandText = "select * from fileTable where fileContent like '%" + tbxDoond.Text + "%'";
                        SqlDataReader dr = comm.ExecuteReader();
                        DoondList.DataSource = dr;


                        DoondList.DataBind();
                        dr.Close();
                        comm.CommandText = "Select * from fileTable where fileContent like '%" + tbxDoond.Text + "%'";
                        SqlDataReader dr1 = comm.ExecuteReader();
                        bool found = false;
                        int count = 0;
                        while (dr1.Read())
                        {
                            found = true;
                            count++;
                        }
                        if (!found)
                        {
                            lblError.Text = count + " Record(s) Found";
                        }
                        else
                        {
                            
                            lblError.Text = count + " Record(s) Found For " + tbxDoond.Text;
                        }
                    }

                }
                catch (SqlException ex)
                {
                    lblError.Text += ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }
        }

    }
}
